package com.tarena.lbs.business.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.business.dao.mapper.StoreMapper;
import com.tarena.lbs.business.dao.repository.StoreRepository;
import com.tarena.lbs.business.pojo.param.ChainQuery;
import com.tarena.lbs.business.pojo.po.StorePO;
import com.tarena.lbs.business.pojo.query.AreaStoreQuery;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class StoreRepositoryImpl implements StoreRepository {
    @Autowired
    private StoreMapper storeMapper;
    @Override
    public PageInfo<StorePO> pageList(StoreQuery query) {
        QueryWrapper<StorePO> queryWrapper = assembleAdminQuery(query);
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<StorePO> pos = storeMapper.selectList(queryWrapper);
        return new PageInfo<>(pos);
    }

    @Override
    public void save(StorePO po) {
        log.info("新增店铺，入参:{}",po);
        storeMapper.insert(po);
    }

    @Override
    public List<StorePO> storeEdit(ChainQuery query) {
        QueryWrapper<StorePO> queryWrapper = new QueryWrapper<>();
        if (query.getStoreName() != null){
            queryWrapper.like("store_name",query.getStoreName());
        }
        queryWrapper.eq("business_id",query.getBusinessId());
        return storeMapper.selectList(queryWrapper);
    }

    @Override
    public void updateStatus(StorePO po) {
        log.info("更新店铺状态，入参:{}",po);
        storeMapper.updateById(po);
    }

    @Override
    public StorePO findById(Integer id) {
        // 添加参数校验
        if (id == null || id <= 0) {
            log.warn("查询店铺时ID参数无效: {}", id);
            return null;
        }
        
        StorePO storePO = storeMapper.selectById(id);
        // 添加空值检查
        if (storePO == null) {
            log.warn("未找到ID为{}的店铺", id);
            return null;
        }
        
        return storePO;
    }

    @Override
    public void deleteById(Integer id) {
        log.info("删除店铺，入参:{}",id);
        storeMapper.deleteById(id);
        log.info("删除店铺，出参:{}",id);
    }

    @Override
    public PageInfo<StorePO> getStoreByCity(AreaStoreQuery query) {
        QueryWrapper<StorePO> queryWrapper = new QueryWrapper<>();
        if (query.getCityIdList() != null && query.getCityIdList().size() > 2 && query.getCityIdList().get(2) != null) {
            queryWrapper.eq("city_id", query.getCityIdList().get(2));
        }
        if (query.getCityIdList() != null && query.getCityIdList().size() > 1 && query.getCityIdList().get(1) != null) {
            queryWrapper.eq("area_id", query.getCityIdList().get(1));
        }
        if (query.getCityIdList() != null && query.getCityIdList().size() > 0 && query.getCityIdList().get(0) != null) {
            queryWrapper.eq("province_id", query.getCityIdList().get(0));
        }
        queryWrapper.eq("business_id",query.getBusinessId());
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<StorePO> pos = storeMapper.selectList(queryWrapper);
        return new PageInfo<>(pos);
    }

    private QueryWrapper<StorePO> assembleAdminQuery(StoreQuery query) {
        QueryWrapper<StorePO> queryWrapper = new QueryWrapper<>();
        //1.accountPhone 账号手机号 相等查询 where account_phone=#{phone}
        if (StringUtils.isNotBlank(query.getStoreName())){
            queryWrapper.like("store_name",query.getStoreName());
        }
        if (StringUtils.isNotBlank(query.getStoreHeadPhone())){
            queryWrapper.like("store_head_phone",query.getStoreHeadPhone());
        }
        if (query.getStoreStatus() != null){
            queryWrapper.eq("store_status",query.getStoreStatus());
        }
        if (query.getStartingTime() != null){
            queryWrapper.ge("update_time",query.getStartingTime());
        }
        if (query.getEndTime() != null){
            queryWrapper.le("update_time",query.getEndTime());
        }
        if (query.getProvinceId() != null){
            queryWrapper.eq("province_id",query.getProvinceId());
        }
        if (query.getCityId() != null){
            queryWrapper.eq("city_id",query.getCityId());
        }
        if (query.getAreaId() != null){
            queryWrapper.eq("area_id",query.getAreaId());
        }
        queryWrapper.eq("business_id",query.getBusinessId());
        return queryWrapper;
    }
}
